package icu.javacg.storage.controller;

import icu.javacg.storage.pojo.Environment;
import icu.javacg.storage.pojo.vo.ReturnResult;
import icu.javacg.storage.service.EnvironmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 郝文韬
 *
 * @author sky
 * @date: 2023/4/24 11:36
 */

@RestController
@CrossOrigin
@Tag(name = "环境管理接口-郝文韬")
@RequestMapping("/environment")
public class EnvironmentController {

    @Resource
    private EnvironmentService environmentService;

    @GetMapping("/getAll")
    @Operation(summary = "所有记录的环境温度")
    public ReturnResult<List<Environment>> getEnvironment() {
        List<Environment> environmentList = environmentService.list();
        if (environmentList.size() == 0) {
            return new ReturnResult<>(1000, "环境温度湿度全查失败，没有数据");
        }
        return new ReturnResult<>(1500, "环境温度湿度全查成功", environmentService.list());
    }

    @GetMapping("/getEnvironmentByHumidityValue")
    @Operation(summary = "通过湿度值获取环境数据")
    public ReturnResult<List<Environment>> getEnvironmentByHumidityValue(
            @RequestParam("humidityValue") Double humidityValue) {
        return environmentService.getEnvironmentByHumidityValue(humidityValue);
    }

    @GetMapping("/getEnvironmentByTemperatureValue")
    @Operation(summary = "通过温度值获取环境数据")
    public ReturnResult<List<Environment>> getEnvironmentByTemperatureValue(
        @RequestParam("temperatureValue") Double temperatureValue){
            return environmentService.getEnvironmentByTemperatureValue(temperatureValue);
    }

    @PostMapping ("/getEnvironmentByCondition")
    @Operation(summary = "通过条件查询环境数据")
    public ReturnResult<List<Environment>> getEnvironmentByCondition(
            @RequestBody Environment environment) {
        return environmentService.getEnvironmentByCondition(environment);
    }

    @PutMapping("/addEnvironment")
    @Operation(summary = "添加环境数据")
    public ReturnResult<Environment> addEnvironment(
            @RequestBody Environment environment) {
        return environmentService.addEnvironment(environment);
    }

    @PostMapping("/updateEnvironment")
    @Operation(summary = "修改环境数据")
    public ReturnResult<Boolean> updateEnvironment(
            @RequestBody Environment environment) {
        boolean update = environmentService.updateById(environment);
        if (update) {
            return new ReturnResult<>(1505, "修改成功", update);
        }else {
            return new ReturnResult<>(1005, "修改失败", update);
        }
    }

    @DeleteMapping("deleteEnvironment")
    @Operation(summary = "删除环境数据")
    public ReturnResult<Boolean> deleteEnvironment(
            @RequestParam("id") Integer id) {
        boolean delete = environmentService.removeById(id);
        if (delete) {
            return new ReturnResult<>(1506, "删除成功", delete);
        }else {
            return new ReturnResult<>(1006, "删除失败", delete);
        }
    }
}